#include "TestListBoxForm.h"
#include <Zaf\String\Trim.h>


#define IDC_LISTBOX 0xf001
#define IDC_TXT_INPUT 0xf002
#define IDC_BTN_INPUT 0xf003
#define IDC_BTN_DELETE 0xf004


#define IDC_CHK_SORT 0x1
#define IDC_CHK_SHOWSCROLL 0x2
#define IDC_CHK_MULTICOLUMN 0x4
#define IDC_RAD_NONE 0x8
#define IDC_RAD_SINGLE 0x10
#define IDC_RAD_MULTISIMPLE 0x20
#define IDC_RAD_MULTIEXTEND 0x40


struct ControlInfo {
	WORD ID;
	const wchar_t* Text;
};

//static struct {
//
//	WORD ID;
//	const wchar_t* Text;
//
//} ButtonInfos[] = {
//
//	IDC_BTN_SHOWSCROLL, L"ShowScroll",
//};


static ControlInfo CheckBoxInfos[] = {

	IDC_CHK_SORT, L"Sort",
	IDC_CHK_SHOWSCROLL, L"ShowScroll",
	IDC_CHK_MULTICOLUMN, L"MultiCol",
};

static ControlInfo RadioButtonInfos[] = {

	IDC_RAD_NONE, L"None",
	IDC_RAD_SINGLE, L"Single",
	IDC_RAD_MULTISIMPLE, L"MultiSimple",
	IDC_RAD_MULTIEXTEND, L"MultiExtend",
};


TestListBoxForm::TestListBoxForm() : 
	m_btnInput(IDC_BTN_INPUT),
	m_txtInput(IDC_TXT_INPUT),
	m_btnDelete(IDC_BTN_DELETE)
{
	m_styleFlag = 0;
	m_listBox = nullptr;
	CreatedEvent.Register(this, &TestListBoxForm::OnCreated);
}



void TestListBoxForm::OnCreated(CreatedEventArgs&) {

	this->SetSize(500, 400);

	m_txtInput.SetParent(*this);
	m_txtInput.SetSize(150, 30);
	m_txtInput.SetPosition(0, 200);
	m_txtInput.Create();

	m_btnInput.SetText(L"+");
	m_btnInput.SetParent(*this);
	m_btnInput.SetSize(50, 30);
	m_btnInput.SetPosition(150, 200);
	m_btnInput.Create();

	m_btnDelete.SetText(L"-");
	m_btnDelete.SetParent(*this);
	m_btnDelete.SetSize(50, 30);
	m_btnDelete.SetPosition(200, 200);
	m_btnDelete.Create();

	int x = 0;

	for (int index = 0; index < CheckBoxCount; ++index) {

		m_checkBoxs[index].SetID(CheckBoxInfos[index].ID);
		m_checkBoxs[index].SetText(CheckBoxInfos[index].Text);
		m_checkBoxs[index].SetSize(100, 30);
		m_checkBoxs[index].SetPosition(x, 250);
		m_checkBoxs[index].SetParent(*this);
		m_checkBoxs[index].Create();

		x += 100;
	}

	x = 0;

	for (int index = 0; index < RadioButtonCount; ++index) {

		m_radioButtons[index].SetID(RadioButtonInfos[index].ID);
		m_radioButtons[index].SetText(RadioButtonInfos[index].Text);
		m_radioButtons[index].SetSize(100, 30);
		m_radioButtons[index].SetPosition(x, 280);
		m_radioButtons[index].SetParent(*this);
		m_radioButtons[index].Create();

		x += 100;
	}

	m_radioButtons[1].IsSelected(true);

	CreateListBox();
}



LRESULT TestListBoxForm::WndProc(const WndMsg& msg) {

	switch (msg.Message) {

	case WM_COMMAND:

		if (msg.WParamHigh == BN_CLICKED) {

			for (int index = 0; index < CheckBoxCount; ++index) {

				if (msg.WParamLow == m_checkBoxs[index].GetID()) {

					if (m_checkBoxs[index].GetCheckState() == CheckState::Checked) {
						m_styleFlag |= CheckBoxInfos[index].ID;
					}
					else {
						m_styleFlag &= ~CheckBoxInfos[index].ID;
					}

					CreateListBox();

					return 0;
				}
			}

			switch (msg.WParamLow) {

			case IDC_BTN_INPUT: {

				std::wstring text = m_txtInput.GetText();
				Zaf::String::Trim(text);

				if (text.empty() == false) {
					m_listBox->AddString(text);
				}
				return 0;
								}

			case IDC_BTN_DELETE: {

				SelectionMode selMode = m_listBox->GetSelectionMode();

				if (selMode == SelectionMode::Single) {
					m_listBox->DeleteString(m_listBox->GetSelectedIndex());
				}
				else if (selMode == SelectionMode::MultiSimple || selMode == SelectionMode::MultiExtended) {

					int itemIndex;

					while (m_listBox->GetSelectedIndices(1, &itemIndex) != 0) {
						m_listBox->DeleteString(itemIndex);
					}
				}
				return 0;
								 }

			case IDC_RAD_NONE:
				m_styleFlag &= ~(IDC_RAD_NONE | IDC_RAD_SINGLE | IDC_RAD_MULTISIMPLE | IDC_RAD_MULTIEXTEND);
				m_styleFlag |= IDC_RAD_NONE;
				CreateListBox();
				return 0;

			case IDC_RAD_SINGLE:
				m_styleFlag &= ~(IDC_RAD_NONE | IDC_RAD_SINGLE | IDC_RAD_MULTISIMPLE | IDC_RAD_MULTIEXTEND);
				m_styleFlag |= IDC_RAD_SINGLE;
				CreateListBox();
				return 0;

			case IDC_RAD_MULTISIMPLE:
				m_styleFlag &= ~(IDC_RAD_NONE | IDC_RAD_SINGLE | IDC_RAD_MULTISIMPLE | IDC_RAD_MULTIEXTEND);
				m_styleFlag |= IDC_RAD_MULTISIMPLE;
				CreateListBox();
				return 0;

			case IDC_RAD_MULTIEXTEND:
				m_styleFlag &= ~(IDC_RAD_NONE | IDC_RAD_SINGLE | IDC_RAD_MULTISIMPLE | IDC_RAD_MULTIEXTEND);
				m_styleFlag |= IDC_RAD_MULTIEXTEND;
				CreateListBox();
				return 0;
			}
		}
		break;
	}

	return Form::WndProc(msg);
}



void TestListBoxForm::CreateListBox() {

	if (m_listBox != nullptr) {
		delete m_listBox;
	}

	m_listBox = new ListBox(IDC_LISTBOX);

	m_listBox->IsAutoSort(m_styleFlag & IDC_CHK_SORT);
	m_listBox->AlwaysShowScrollbar(m_styleFlag & IDC_CHK_SHOWSCROLL);
	m_listBox->IsMultiColumn(m_styleFlag & IDC_CHK_MULTICOLUMN);

	if (m_styleFlag & IDC_RAD_NONE) {
		m_listBox->SetSelectionMode(SelectionMode::None);
	}
	else if (m_styleFlag & IDC_RAD_SINGLE) {
		m_listBox->SetSelectionMode(SelectionMode::Single);
	}
	else if (m_styleFlag & IDC_RAD_MULTISIMPLE) {
		m_listBox->SetSelectionMode(SelectionMode::MultiSimple);
	}
	else if (m_styleFlag & IDC_RAD_MULTIEXTEND) {
		m_listBox->SetSelectionMode(SelectionMode::MultiExtended);
	}

	m_listBox->Create();
	m_listBox->SetParent(*this);
	m_listBox->SetSize(300, 200);
	m_listBox->SetPosition(0, 0);
}